package com.gopay.scheduler.dao.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.Constants;
import com.gopay.common.constants.fund.FundSettleStatusEnum;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.domain.fund.FundGopayFileEntity;
import com.gopay.scheduler.dao.BaseOperateDao;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Repository("baseOperateCornDao")
public class BaseOperateDaoImpl extends BaseDAOImpl<Object[], Serializable> implements BaseOperateDao {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseOperateDaoImpl.class);
    @Override
    public void update02018OrderForSettle(String gopayOrderId,String orderStatus,int orderStatusMain) {
        LOGGER.info("同步02018订单状态为成功，开始！");
        Map<String, Object> map = new HashMap<>();
        StringBuffer sql = new StringBuffer("UPDATE FUND_GOPAY_CPS_GEN_ORDER set ");
        Date lastUpdTs = new Date();
        if(lastUpdTs!=null && StringUtils.isNotEmpty(gopayOrderId)){
            sql.append(" LAST_UPD_TS=:lastUpdTs ");
            map.put("lastUpdTs", lastUpdTs);
            sql.append(" ,ORDER_TYPE=:orderStatus ");
            map.put("orderStatus", orderStatus);
            if(FundSettleStatusEnum.SUCCESS_SETTLE.getValue().equals(orderStatus)){
                sql.append(" ,MEMO=:memo ");
                map.put("memo", "success");
                sql.append(",LAST_ERROR_MSG='成功'");
            }
            sql.append(" WHERE GOPAY_ORDER_ID=:gopayOrderId ");
            map.put("gopayOrderId", gopayOrderId);
            updateBySql(sql.toString(), map);
        }
        Map<String, Object> mapMain = new HashMap<>();
        StringBuffer sqlMain = new StringBuffer("UPDATE CPS_GEN_MAIN_ORDER set ");
        if(lastUpdTs!=null && StringUtils.isNotEmpty(gopayOrderId)){
            sqlMain.append(" LAST_UPD_TS=:lastUpdTs ");
            mapMain.put("lastUpdTs", lastUpdTs);
            sqlMain.append(" ,TXN_STA_CD=:orderStatus ");
            mapMain.put("orderStatus", orderStatusMain);
            if(TxnStaCode.TXN_STA_CD_20000.value == orderStatusMain) {
                sqlMain.append(" ,OUT_STLM_ID=:outStlmId ");
                mapMain.put("outStlmId", Constants.GFB_BANK_CODE);
                sqlMain.append(" ,ORDER_STLM_TIME=:orderStlmTime ");
                mapMain.put("orderStlmTime", lastUpdTs);
            }
            sqlMain.append(" ,TRAN_FINISH_TIME=:tranFinishTime ");
            mapMain.put("tranFinishTime", lastUpdTs);
            sqlMain.append(" WHERE GOPAY_ORDER_ID=:gopayOrderId ");
            mapMain.put("gopayOrderId", gopayOrderId);
            updateBySql(sqlMain.toString(), mapMain);
        }
        LOGGER.info("同步02018订单状态为成功，结束！");
    }

    @Override
    public void updateFile(FundGopayFileEntity fileEntity) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("filePath",fileEntity.getFilePath());
        paramMap.put("fileType",fileEntity.getFileType());
        paramMap.put("keyId",fileEntity.getKeyId());
        updateBySql("UPDATE FUND_GOPAY_FILE SET FILE_PATH=:filePath,FILE_TYPE=:fileType WHERE KEY_ID=:keyId",paramMap);
    }
}
